/* 
 * Copyright 2010 Ziya Guven Koylu, Ozge Koylu
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.koylu.caffein.model.clazz;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javassist.util.proxy.ProxyFactory;
import javassist.util.proxy.ProxyObject;

import com.koylu.caffein.exception.ClazzConfigException;
import com.koylu.caffein.model.caffein.CaffeinConfig;
import com.koylu.caffein.model.clazz.validation.ValidateFor;
import com.koylu.caffein.model.clazz.validation.Validation;
import com.koylu.caffein.model.container.Container;
import com.koylu.caffein.model.container.QueryPropertyContainer;
import com.koylu.caffein.model.handler.QueryPropertyContainerHandler;
import com.koylu.caffein.query.filter.CompositeFilter;
import com.koylu.caffein.query.group.Group;
import com.koylu.caffein.query.join.Join;
import com.koylu.caffein.query.order.Order;
import com.koylu.caffein.query.target.Target;

public class QueryProperty implements Field{
	
	private String name;
	private Node parent;
	private Class<?> proxyClass;
	private String clazz;
	private String alias;
	private List<Join> joins;
	private CompositeFilter compositeFilter;
	private List<Order> orders;
	private List<Group> groups;
	private Target target;
	private int firstResult = -1;
	private int maxResult = -1;
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public Node getParent() {
		return parent;
	}

	public void setParent(Node parent) {
		this.parent = parent;
	}
	
	public String getClazz() {
		return clazz;
	}

	public void setClazz(String clazz) {
		this.clazz = clazz;
	}

	public String getAlias() {
		return alias;
	}

	public void setAlias(String alias) {
		this.alias = alias;
	}

	public int getFirstResult() {
		return firstResult;
	}
	
	public void setFirstResult(int firstResult) {
		this.firstResult = firstResult;
	}

	public int getMaxResult() {
		return maxResult;
	}
	
	public void setMaxResult(int maxResult) {
		this.maxResult = maxResult;
	}
	
	public void addJoin(Join join) {
		if(joins == null){
			joins = new ArrayList<Join>();
		}
		joins.add(join);
	}
	
	public void setCompositeFilter(CompositeFilter compositeFilter) {
		this.compositeFilter = compositeFilter;
	}
	
	public CompositeFilter getCompositeFilter() {
		return compositeFilter;
	}

	public void addOrder(Order order) throws Exception {
		if(orders == null){
			orders = new ArrayList<Order>();
		}
		orders.add(order);
	}
	
	public void addGroup(Group group) throws Exception {
		if(groups == null){
			groups = new ArrayList<Group>();
		}
		groups.add(group);
	}
	
	public void setTarget(Target target){
		this.target = target;
	}
	
	public Target getTarget() {
		return target;
	}

	public Collection<? extends Node> getChildren() {
		return null;
	}

	public Object toObject(CaffeinConfig caffeinConfig, Object object, ResultSet resultSet) throws Exception {
		Clazz clazzz = caffeinConfig.getClazz(clazz);
		if(clazzz == null){
			throw new ClazzConfigException("clazz not found:"+clazz, this);
		}
		Object newObject = getProxyClass(caffeinConfig).newInstance();
		((ProxyObject) newObject).setHandler(new QueryPropertyContainerHandler(caffeinConfig, clazzz, object, alias, joins, compositeFilter, orders, groups, target, firstResult, maxResult));
		return newObject;
	}
	
	public Class<?> getProxyClass(CaffeinConfig caffeinConfig) throws Exception {
		if(proxyClass == null){
			ProxyFactory proxyFactory = new ProxyFactory();
			proxyFactory.setSuperclass(toVirtualClass(caffeinConfig));
			proxyFactory.setInterfaces(QueryPropertyContainer.interfaces);
			proxyFactory.setFilter(Container.methodFilter);
			proxyClass = proxyFactory.createClass();
		}
		return proxyClass;
	}

	public Object toEmptyObject(CaffeinConfig caffeinConfig, Clazz clazz) throws Exception {
		return null;
	}

	public Class<?> toVirtualClass(CaffeinConfig caffeinConfig) throws Exception {
		return Object.class;
	}

	public Object copy(CaffeinConfig caffeinConfig, Object object) throws Exception {
		Object newObject = getProxyClass(caffeinConfig).newInstance();
		((ProxyObject) newObject).setHandler(new QueryPropertyContainerHandler(((QueryPropertyContainer)object).getQuery()));
		return newObject;
	}

	public void setValidations(List<Validation> validations) {}

	public List<Validation> getValidations() {
		return null;
	}

	public void validate(CaffeinConfig caffeinConfig, Clazz clazz, Object object, ValidateFor validateFor) throws Exception {}
	
	public String getPath(CaffeinConfig caffeinConfig) throws Exception {
		return name;
	}
	
	public String toString() {
		return getClass().getName()+"["+name+"]";
	}
}